// pages/demand/demand.js
const ajax = require('../../../utils/quest.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShow: true,
    cateList: [],
    idx: 0,
    index: 0,
    cateSubList: [],
    isShow: 0,
    searchList: [{
        id: 1,
        name: '品类'
      },
      {
        id: 2,
        name: '距离'
      },
      {
        id: 3,
        name: '星级'
      },
    ],
    checkedData: 0,
    dataList: [],
    recommendListData: [],
    ifHaveMore: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      searchList: [{
          id: 1,
          name: '品类'
        },
        {
          id: 2,
          name: '距离'
        },
        {
          id: 3,
          name: '星级'
        },
      ],
      idx: options.id
    })
    this.getTree()
    this.getListServiceProvider()
  },
  getFilter: function (e) {
    var type = e.currentTarget.dataset.type;
    var isShow = this.data.isShow
    // type:1距离;2销量;3星级;
    if (type == 1) {
      this.setData({
        isShow: 1,
        selActive: 1,

      })
      if (isShow == 1) {
        this.setData({
          isShow: 0,
          selActive: 1,
        })
      }
    } else if (type == 2) {
      this.setData({
        selActive: 2,
      })
    } else if (type == 3) {
      this.setData({
        selActive: 3,
      })
    }
  },
  navbarTap: function (e) {
    console.log("EEEEE", e);
    var index = e.currentTarget.dataset.id.id
    const categoryName = e.currentTarget.dataset.id.categoryName
    var sonCategoryList = e.currentTarget.dataset.id.sonCategoryList
    let that = this
    that.setData({
      idx: index,
      cateSubList: sonCategoryList,
      searchList: [{
          id: 1,
          name: categoryName
        },
        {
          id: 2,
          name: '距离'
        },
        {
          id: 3,
          name: '星级'
        },
      ],
    })
  },
  addressRelicList: function (e) {
    var id = e.currentTarget.dataset.id;
    console.log(id);
    this.setData({
      checkedData: id,
      idx: id,
      dataList: [],
      isShow: 0
    })
    this.getListServiceProvider()
  },
  getTree: function (e) {

    ajax.sendRequest('/category/getCategoryTree', 'get').then(res => {
      const {
        categoryVOList
      } = res.data
      this.setData({
        cateList: categoryVOList,
      })
    })

  },
  getListServiceProvider() {
    const {
      idx
    } = this.data
    ajax.sendRequest('/findService/listServiceProvider?categoryId=' + idx, 'get').then(res => {
      const dataList = this.data.dataList
      const list = res.data.page.list
      if (list.length) {
        for (var i = 0; i < list.length; i++) {
          dataList.push(res.data.page.list[i])
        }
      }
      this.setData({
        recommendListData: dataList,
        ifHaveMore: true,
      })
    })
  },
  bandDetail(e) {
    console.log(e);
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/seeksService/merchantDetail/index?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */

  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const {
      ifHaveMore
    } = this.data
    if (ifHaveMore) {
      this.setData({
        page: this.data.page + 1
      })
      this.getRecommendList()
    } else {
      wx.showToast({
        title: '暂无更多数据',
        icon: "none"
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})